package 优先算法.滑动窗口.最大连续1的个数3;

/**
 * <a href="https://leetcode.cn/problems/max-consecutive-ones-iii/submissions/532523071/"
 */
public class Max {
    public static void main(String[] args) {
    Solution so = new Solution();
    int[] nums = {1,1,1,0,0,0,1,1,1,1,0};
    int[] nums2 = {0,0,0,1};

    int rel = so.longestOnes(nums, 2);
    int rel2 = so.longestOnes(nums2, 4);
    System.out.println(rel);
    System.out.println(rel2);

    }
}
class Solution {
    public int longestOnes(int[] nums, int k) {
        int zero=0,len=0,left=0,right=0,n=nums.length;
        while(right<n){
            //进窗口
            if(nums[right++] == 0) {
                zero++;
            }

            while(zero>k){//出窗口

                if(nums[left++]==0) {
                    zero--;
                }
            }
            //更新
            len=Math.max(len,right-left);
        }
        return len;
    }
}
